package com.hope.minio.controller;

import com.hope.minio.common.result.Result;
import com.hope.minio.common.result.ResultUtil;
import com.hope.minio.config.MinioConfig;
import com.hope.minio.model.FileChunk;
import com.hope.minio.service.FileUploadService;
import com.hope.minio.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RequestMapping("/minio")
@RestController
@Api(tags = "文件上传接口")
public class FileController {

    @Autowired
    private MinioService minioService;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private MinioConfig minioConfig;

//    minio.GET("/get_chunks", minioService.GetSuccessChunks)
//            minio.GET("/new_multipart", minioService.NewMultipart)
//            minio.GET("/get_multipart_url", minioService.GetMultipartUploadUrl)
//            minio.POST("/complete_multipart", minioService.CompleteMultipart)
//            minio.POST("/update_chunk", minioService.UpdateMultipart)

    @ApiOperation(value = "获取chunks")
    @PostMapping("/get_chunks")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "md5", value = "md5", required = true),
    })
    public Result<FileChunk> getSuccessChunks(String md5) {
        return fileUploadService.getSuccessChunks(md5);

    }

    @ApiOperation(value = "注册文件")
    @PostMapping("/new_multipart")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "FileChunk", name = "fileChunk", value = "fileChunk", required = true),
    })
    public Result<FileChunk> newMultipart(FileChunk fileChunk) {
        return fileUploadService.newMultipart(fileChunk);
    }


    @ApiOperation(value = "获取上传url")
    @PostMapping("/get_multipart_url")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "FileChunk", name = "fileChunk", value = "fileChunk", required = true),
    })
    public Result<String> getMultipartUrl(FileChunk fileChunk) {
        return fileUploadService.getMultipartUrl(fileChunk);
    }
    @ApiOperation(value = "上传完成")
    @PostMapping("/complete_multipart")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "FileChunk", name = "fileChunk", value = "fileChunk", required = true),
    })
    public Result<String> completeMultipart(FileChunk fileChunk) {
        return fileUploadService.completeMultipart(fileChunk);
    }

    public Result uploadIputSreamFile(MultipartFile file, String bucketName) {
        try {
            bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
            if (!minioService.bucketExists(bucketName)) {
                minioService.makeBucket(bucketName);
            }
            String fileName = file.getOriginalFilename();
            String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date()) + UUID.randomUUID().toString().replaceAll("-", "")
                    + fileName.substring(fileName.lastIndexOf("."));

            InputStream inputStream = file.getInputStream();
            minioService.putObject(bucketName, objectName, inputStream, MimeTypeUtils.IMAGE_JPEG_VALUE);
            inputStream.close();
            return ResultUtil.success(minioService.getObjectUrl(bucketName, objectName));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.sendErrorMessage("上传失败");
        }
    }
}
